<?php

  class PiankuAction extends CommonAction{
  
    public function index(){
	
	  $where['dailiid']=array('eq','0');
	
	  $re=$this->page('pianku','10','id desc',$where);
	  
	   $i=-1;
	  foreach($re as $v){
	    $i++;
		
		$result=M('mypianku')->where('uid="%s" and pid="%s"',session('did'),$v['id'])->find();
		
		if($result!=null){
		
		  $re[$i]['ishave']=1;
		
		}else{
		
		  $re[$i]['ishave']=0;
		
		}
	  
	  }
	  
	  
	
	  $this->re=$re;
	  
	  $this->yuangong=M('yuangong')->where('uid="%s"',session('did'))->select();
	
	  $this->display();
	
	}
  
    public function my(){
	
	  $where['uid']=array('eq',session('did'));
	
	  $re=$this->page("mypianku",'10','id desc',$where);
	  
	  $i=-1;
	  
	  foreach($re as $v){
	    $i++;
		
		$result=M('pianku')->where('id="%s"',$v['pid'])->find();
		
		$re[$i]['video']=$result['video'];
		
		$re[$i]['title']=$result['title'];
		
		
		if($result['dailiid']!=0){
		
		  $re[$i]['shuxing']="私有";
		
		}else{
		
		  $re[$i]['shuxing']="公有";
		
		}
		
		
		
		$yuangong=M('yuangong')->where('id="%s"',$v['yid'])->find();
		
		$re[$i]['yuangong']=$yuangong['username'];
		
		
	  
	  
	  }
	 
	
	  $this->re=$re;
	
	  $this->display();
	
	}
	
	public function add(){
	
	  $arr['pid']=$_POST['id'];
	  
	  $arr['uid']=session('did');
	  
	  $arr['time']=time();
	  
	  $arr['yingli']=0;
	  
	 
	  
	  //生成短连接
	  $sys=M('system')->where('id=1')->field('tiaozhuanlianjie')->find();
	  
	  $re=M('mypianku')->add($arr);
	  
	  
	  $longurl=$sys['tiaozhuanlianjie']."/index.php/tiaozhuan/index/uid/".session('did')."/id/".$_POST['id']."/pid/".$re."/";
	  
	  $data['url']=$this->duanlianjie($longurl);
	  
	  M('mypianku')->where('id="%s"',$re)->save($data);
	  
	  
	  
	  if($re){
	  
	    $ajax=1;
	  }else{
	  
	    $ajax=2;
	  }
	  
	  $this->ajaxReturn($ajax);
	
	}
	
	
	
	public function piliang(){
	
	  $sys=M('system')->where('id=1')->field('tiaozhuanlianjie')->find();
	
	  $where['uid']=array('eq',session('did'));
	  
	  $res=M('mypianku')->where($where)->select();
	  
	  foreach($res as $v){
	  
	       $longurl=$sys['tiaozhuanlianjie']."/index.php/tiaozhuan/index/uid/".session('did')."/id/".$v['pid']."/pid/".$v['id']."/";
		   
		   
		    $data['url']=$this->duanlianjie($longurl);
	  
	       $re= M('mypianku')->where('id="%s"',$v['id'])->save($data);
	  
	  
	  }
	  
	  if($re!==false){
	  
	    $ajax=1;
	  
	  }else{
	  
	    $ajax=2;
	  
	  }
	
	   $this->ajaxReturn($ajax);
	}
	
	public function big(){
	
	  $this->re=M('pianku')->where('id="%s"',$_GET['id'])->find();
	  
	  $this->sys=M('system')->where('id=1')->find();
	  
	  layout(false);
	  
	  $this->display();
	
	}
  
  
    public function del(){
	
	  $re=M('mypianku')->where('id="%s"',$_POST['id'])->find();
	  
	  if($re['uid']!=session('did')){
	  
	    $ajax=3;
		
		$this->ajaxReturn($ajax);
	  
	  }else{
	  
	  
	    $result=M('mypianku')->where('id="%s"',$_POST['id'])->delete();
		
		if($result!==false){
		
		  $ajax=1;
		}else{
		  $ajax=2;
		}
	  
	    $this->ajaxReturn($ajax);
	  
	  }
	  
	
	}
	
	
	public function daochu(){
	
	   header("Content-type: text/html; charset=utf-8");   
	
	  if(!empty($_POST)){
	  
	  
	    if($_POST['leixing']==0){
	  
	  
			foreach($_POST['ids'] as $v){
			
			  $mypk=M('mypianku')->where('id="%s"',$v)->find();
			  
			  
			  $pk=M('pianku')->where('id="%s"',$mypk['pid'])->find();
			  
			 
			  
			 // dump($mypk);
			  
			  echo $pk['title']."<br/>";
			  echo $mypk['url']."</br/>";
			}
			
			
			
			echo "<br/> 
			  <input  onclick='history.go(-1)' value='返回' type='button'>
			";
		
		
		}else{
		
		   foreach($_POST['ids'] as $v){
			
			  $re=M('mypianku')->where('id="%s"',$v)->delete();
			  
			}
		    
			if($re){
			
			  $this->success('删除成功');exit();
			
			
			}else{
			
			  $this->error('删除失败');
			
			
			}
		
		
		}
		
	  
	  }else{
	  
	    $this->error('您没选择任何影片');
	  
	  }
	
	}
	
	
	
	public function xiangxi(){
	
	  $where['status']=array('eq','1');
	  $where['pid']=array('eq',$_GET['id']);
	  $re=$this->page('buyer','10','id desc',$where);
	  $this->re=$re;
	  
	  $this->display();
	
	}
	
	
	public  function saveprice(){
	
	
	   $this->ajaxReturn((M('mypianku')->save($_POST))?1:2);
	
	
	}
	
	
  
  
  }